<?php

namespace addons\yiliao\controller\api;

use app\admin\model\yiliao\Doctor as DoctorModel;
use app\admin\model\yiliao\Shop;
use app\admin\model\yiliao\Pblist;
use app\admin\model\yiliao\Pbholiday;
use app\admin\model\yiliao\Booking;
use think\Db;

class Doctor extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    //会议室  展厅 茶水间  shopid区分
    public function homeDoctor()
    {
        $isshop = $this->request->param('isshop', 0);
        $shopid = $this->request->param('shopid');
		$scene = $this->request->param('scene');
        $where=[];
        $where['status']='normal';
        $where['homedata']=1;
//        if ($isshop==1) {
//			if($scene==1154){
//				$shop = Shop::where(['status'=>'normal'])->order("weigh desc")->find();
//				if($shop){
//					$shopid = $shop['id'];
//				}
//			}
//            $where[] = ['exp',Db::raw("FIND_IN_SET('$shopid',yiliao_shop_ids)")];
//        }
        if($shopid){
            $where[] = ['exp',Db::raw("FIND_IN_SET('$shopid',yiliao_shop_ids)")];
        }
        $rows = DoctorModel::where($where)->order('id desc')->field('id,name,avatar,jibie,tags,area,ename')->select();
        foreach ($rows as $k => $v) {
            $rows[$k]['avatar'] = cdnurl($v['avatar'], true);
        }
        $this->success('', $rows);
    }

    public function doctorList()
    {
        $isshop = $this->request->param('isshop', 0);
        $shopid = $this->request->param('shopid');
        $where=[];
        $where['status']='normal';
        if ($isshop==1) {
            $where[] = ['exp',Db::raw("FIND_IN_SET('$shopid',yiliao_shop_ids)")];
        }
        $rows = DoctorModel::where($where)->field('id,name,avatar,jibie,tags,area,yiliao_shop_ids')->select();
        foreach ($rows as $k => $v) {
            $rows[$k]['avatar'] = cdnurl($v['avatar'], true);
            $rows[$k]['shopname'] =Shop::where('id',$v['yiliao_shop_ids'])->value('name');
        }
        $this->success('', $rows);
    }

    public function doctorDetail()
    {
        $id = $this->request->param('id');
        $isshop = $this->request->param('isshop');
        $shopid = $this->request->param('shopid');
        $row = DoctorModel::where(['id'=>$id])->find();
       
        $row['avatar'] = cdnurl($row['avatar'], true);
        $arr=[];
        if ($isshop==1) {
            $shop = Shop::where(['id'=>$shopid])->find();
        } else {
            $shops = Shop::where(['id'=>['in',$row['yiliao_shop_ids']],'status'=>'normal'])->order("weigh desc")->select();
            $shop = $shops[0];
        }

        $row['yiyuan'] = $shop['name'];
        $row['shopid']=$shop['id'];
        $row['ispb'] = $this->setting['ispb'];

        //排班
        $paiban=[];
        $week = ["周日","周一","周二","周三","周四","周五","周六"];
        $nowdate="";
        $times=[];
        if ($row['ispb'] == 1) {
            for ($i=1;$i<=$this->setting['yueyueday'];$i++) {
                $date = date("Y-m-d", strtotime("+".$i."  day"));
                //多门店优化
                $where=[];
                $where['yiliao_doctor_id'] = $id;
                $where['status'] = 1;
                $where['startdate'] = ['elt',$date];
                $where['enddate'] = ['egt',$date];
                if ($isshop==1) {
                    $where['yiliao_shop_id'] = $shopid;
                }
                
                $pblist = Pblist::where($where)->find();
                $pbholiday = Pbholiday::where(['status'=>1,'yiliao_doctor_id'=>$id,'date'=>$date])->find();
                if ($pblist) {
                    //计算周几是否上班
                    //11=周一上午,12=周一下午,21=周二上午,22=周二下午,31=周三上午,32=周三下午,41=周四上午,42=周四下午,51=周五上午,52=周五下午,61=周六上午,62=周六下午,01=周日上午,02=周日下午
                    $arr = explode(",", $pblist['weekdata']);
                    $w = date("w", strtotime($date));
                    //去除请假
                    if ($pbholiday) {
                        if ($pbholiday['shangdata']==1) {
                            $arr = array_diff($arr, [$w.'1']);
                        }
                        if ($pbholiday['xiadata']==1) {
                            $arr = array_diff($arr, [$w.'2']);
                        }
                    }
                    //有排班
                    if (in_array($w.'1', $arr)||in_array($w.'2', $arr)) {
                        if (sizeof($times)==0) {
                            //$nowdate = $date;
//                            $shangtimes = json_decode($pblist['shangtimes'], true);
//                            $xiatimes = json_decode($pblist['xiatimes'], true);
//                            //周几的上午
//                            if (in_array($w.'1', $arr)) {
//                                foreach ($shangtimes as $kk=>$vv) {
//                                    $time = $vv['start'].'-'.$vv['end'];
//                                    $yiyue = Booking::where(["doctorid"=>$id,'date'=>$date,'times'=>$time,'status'=>['in','1,3']])->count();
//                                    $times[]=['time'=>$time, 'num'=>$vv['num'], 'yiyue'=>$yiyue,'check'=>0];
//                                }
//                            }
//                            //周几的下午
//                            if (in_array($w.'2', $arr)) {
//                                foreach ($xiatimes as $kk=>$vv) {
//                                    $time = $vv['start'].'-'.$vv['end'];
//                                    $yiyue = Booking::where(["doctorid"=>$id,'date'=>$date,'times'=>$time,'status'=>['in','1,3']])->count();
//                                    $times[]=['time'=>$time, 'num'=>$vv['num'], 'yiyue'=>$yiyue,'check'=>0];
//                                }
//                            }
                            //自定义
                            $amtimes = json_decode($pblist['amtimes'], true);
                            $pmtimes = json_decode($pblist['pmtimes'], true);
                            //周几的上午
                            if (in_array($w.'1', $arr)) {
                                $time = $amtimes[0]['start'].'-'.$amtimes[0]['end'];
                                $yiyue = Booking::where(["doctorid"=>$id,'date'=>$date,'times'=>$time,'status'=>['in','1,3']])->count();
                                $times[]=['time'=>$time, 'yiyue'=>$yiyue,'check'=>0];
                            }
                            //周几的下午
                            if (in_array($w.'2', $arr)) {

                                $time = $pmtimes[0]['start'].'-'.$pmtimes[0]['end'];
                                $yiyue = Booking::where(["doctorid"=>$id,'date'=>$date,'times'=>$time,'status'=>['in','1,3']])->count();
                                $times[]=['time'=>$time, 'yiyue'=>$yiyue,'check'=>0];
                            }
                            
                        }
                        $paiban[]=['id'=>$i, "date"=>$date, "day"=>date("m.d", strtotime($date)),'name'=>$week[$w],'isSelected'=>$i==1?1:0];
                    }


                }
                
                
            }
        }
        $row['paiban']=$paiban;
        
        $row['times'] = $times;
        $this->success('', $row);
    }

    public function gettimes()
    {
        $times=[];
        $date = $this->request->param('date');
        $id = $this->request->param('id');
        $isshop = $this->request->param('isshop');
        $shopid = $this->request->param('shopid');
        $where=[];
        $where['yiliao_doctor_id'] =$id;
        $where['status']=1;
        $where['startdate'] = ['elt',$date];
        $where['enddate'] = ['egt',$date];
        if ($isshop==1) {
            $where['yiliao_shop_id'] = $shopid;
        }

        $pblist = Pblist::where($where)->find();
        $pbholiday = Pbholiday::where(['status'=>1,'yiliao_doctor_id'=>$id,'date'=>$date])->find();

        $shangtimes = json_decode($pblist['shangtimes'], true);
        $xiatimes = json_decode($pblist['xiatimes'], true);
        //状态:0=未支付,1=已预约,2=已取消,3=已完成
        $arr = explode(",", $pblist['weekdata']);
        $w=date("w", strtotime($date));
        //去除请假
        if ($pbholiday) {
            if ($pbholiday['shangdata']==1) {
                $arr = array_diff($arr, [$w.'1']);
            }
            if ($pbholiday['xiadata']==1) {
                $arr = array_diff($arr, [$w.'2']);
            }
        }

        //自定义
        $amtimes = json_decode($pblist['amtimes'], true);
        $pmtimes = json_decode($pblist['pmtimes'], true);
        //周几的上午
        if (in_array($w.'1', $arr)) {
            $time = $amtimes[0]['start'].'-'.$amtimes[0]['end'];
            $yiyue = Booking::where(["doctorid"=>$id,'date'=>$date,'times'=>$time,'status'=>['in','1,3']])->count();
            $times[]=['time'=>$time, 'yiyue'=>$yiyue,'check'=>0];
        }
        //周几的下午
        if (in_array($w.'2', $arr)) {

            $time = $pmtimes[0]['start'].'-'.$pmtimes[0]['end'];
            $yiyue = Booking::where(["doctorid"=>$id,'date'=>$date,'times'=>$time,'status'=>['in','1,3']])->count();
            $times[]=['time'=>$time, 'yiyue'=>$yiyue,'check'=>0];
        }

        
//        if (in_array($w.'1', $arr)) {
//            foreach ($shangtimes as $kk=>$vv) {
//                $time = $vv['start'].'-'.$vv['end'];
//                $yiyue = Booking::where(["doctorid"=>$id,'date'=>$date,'times'=>$time,'status'=>['in','1,3']])->count();
//                $times[]=['time'=>$time, 'num'=>$vv['num'], 'yiyue'=>$yiyue,'check'=>0];
//            }
//        }
//        if (in_array($w.'2', $arr)) {
//            foreach ($xiatimes as $kk=>$vv) {
//                $time = $vv['start'].'-'.$vv['end'];
//                $yiyue = Booking::where(["doctorid"=>$id,'date'=>$date,'times'=>$time,'status'=>['in','1,3']])->count();
//                $times[]=['time'=>$vv['start'].'-'.$vv['end'], 'num'=>$vv['num'], 'yiyue'=>$yiyue,'check'=>0];
//            }
//        }

        $this->success('', $times);
    }
}
